package com.dome.mvp.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * Time: 4/26/2021 11:22
 * Author: TAO
 * Description:
 */
public class ActivityManageUtils {
    private Stack<Activity> stack;
    private static ActivityManageUtils instance;

    public static ActivityManageUtils getInstance() {
        if (instance == null) {
            synchronized (ActivityManageUtils.class) {
                if (instance == null) {
                    instance = new ActivityManageUtils();
                }
            }
        }
        return instance;
    }

    private ActivityManageUtils() {
        stack = new Stack<>();
    }

    public void pushActivity(Activity activity) {
        if (stack == null) {
            stack = new Stack<>();
        }
        stack.push(activity);
    }

    public void pop() {
        if (stack.isEmpty()) {
            return;
        }
        stack.pop();
    }

    public void popFinish() {
        if (stack.isEmpty()) {
            return;
        }
        stack.pop().finish();
    }

    public Activity currentActivity() {
        if (stack.isEmpty()) {
            return null;
        }
        return stack.peek();
    }


    public void popAll() {
        for (Activity a : stack) {
            a.finish();
        }
    }
}
